package com.xjj.datarources.config;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @name: xjj
 * @date: 2021/6/2 13:04
 */

/**
 * 说明：〈该类初始化创建队列、转发器，并把队列绑定到转发器〉
 */
@Configuration
public class MQConfig {


    //private static Logger log = LoggerFactory.getLogger(ApplicationConfig.class);

    private final static Log log = LogFactory.get();

    //默认直连型交换机
    @Bean
    public Queue queue(){
        return new Queue("queue_work");
    }



    @Bean
    public Queue top1(){
        return new Queue("topic.work1");
    }


    /**
     * String name,
     * boolean durable,     默认false  是否持久化
     * boolean exclusive,   默认false
     * boolean autoDelete   当没有生产者或者消费者使用该队列，则会被删除
     *
     * @return
     */

    @Bean
    public Queue top2(){
        return new Queue("topic.work2");
    }

    @Bean
    DirectExchange getDirectExchange(){
        return new DirectExchange("direct_exchange");
    }

    /**
     * 有三种构造函数
     *                      name            交换机名
     *                      durable         是否持久化
     *                      autoDelete      是否自动删除
     * @return
     */
    @Bean
    TopicExchange getTopicExchange(){
        return new TopicExchange("topic_exchange");
    }


    //多个队列需要多次绑定到一个交换机上,并给给出路由匹配规则
    @Bean
    Binding bindingToTopic(){
        return BindingBuilder.bind(top1()).to(getTopicExchange()).with("topic.work1");
    }

    @Bean
    Binding bindingToTopic2(){
        return BindingBuilder.bind(top2()).to(getTopicExchange()).with("topic.*");
    }

    @Bean
    Binding bindingToDirect(){
        return BindingBuilder.bind(queue()).to(getDirectExchange()).with("direct.work");
    }


}
